package com.csx.ops.service.node;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.yh.csx.bsf.core.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.var;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BaseNode {

    @Getter @Setter
    @XStreamAsAttribute
    private String text;

    @Getter @Setter
    @XStreamAsAttribute
    private String next;

    /**
     * x y位置 示范：100 200
     */
    @Getter @Setter
    @XStreamAsAttribute
    private String location;


    @XStreamOmitField
    StateEngine engine;
    public BaseNode(){};
    public BaseNode(StateEngine engine){
        this.engine=engine;
    }

    public void flow(){
        this.goNextNode();
    }

    public void goNextNode(){
        if(StringUtils.isEmpty(next)){
            throw new StateException("当前节点"+this.getText()+"无下一个节点");
        }
        goNextNode(next);
    }

    protected void goNextNode(String text){
        var nextNode = findNode(text);
        if(nextNode == null){
            throw new StateException("找不到下一个节点:"+text);
        }
        nextNode.flow();
    }

    public List<BaseNode> getLastNodes(){
        return null;
    }

    public BaseNode findNode(String text){
       var find = this.engine.getNodes().parallelStream().filter(c->c.text.equals(text)).findFirst();
       return find.isPresent()? find.get():null;
    }
}
